<template>
  <div>
    <div class="title">我的作品</div>
    <div class="tabs" style="padding: 15px 0">
      <el-radio-group v-model="radio" @change="handleCurrentChange(1)">
        <!-- <el-radio-button label="">全部</el-radio-button> -->
        <el-radio-button label="1">已上架</el-radio-button>
        <el-radio-button label="0">已下架</el-radio-button>
      </el-radio-group>
      <div class="btn pointer" @click="open">上传</div>
    </div>
    <div class="list">
      <div class="col" v-for="item in list" :key="item.id">
        <div class="item">
          <div class="imgDiv"><img :src="item.image_url" alt="" /></div>
          <div class="option">
            <div class="btn pointer" @click="detail(item)">详情</div>
            <div
              class="btn pointer"
              v-if="item.switch === 0"
              @click="handleShow(item, `您确定上架这个作品？`)"
            >
              上架
            </div>
            <div
              class="btn pointer"
              v-else
              @click="handleShow(item, `您确定下架这个作品？`)"
            >
              下架
            </div>
          </div>
        </div>
      </div>
    </div>
    <pagination
      v-if="total > 0"
      :total="total"
      :currentPage="page"
      :pageSize="rows"
      @handleCurrentChange="handleCurrentChange"
    />
    <upload ref="uploadRef" @list-refresh="listRefresh" />
    <el-dialog :visible.sync="dialogVisible" width="600px">
      <div class="content">
        <el-form :model="ruleForm" label-width="60px" class="demo-ruleForm">
          <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
          <el-form-item label="赛事" prop="event_id">
            <el-select v-model="ruleForm.event_id" placeholder="请选择">
              <el-option
                v-for="item in eventList"
                :key="item.id"
                :label="item.title_cn"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="ruleForm.price"></el-input>
          </el-form-item>
          <el-form-item label="图片" prop="image_url">
            <el-image :src="ruleForm.image_url"></el-image>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="imagesUpdate">保存修改</el-button>
            <el-button @click="dialogVisible = false">关闭</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "NavBar",
  data() {
    return {
      radio: "1",
      page: 1,
      rows: 9,
      total: 0,
      list: [],
      userUpload: {},
      ruleForm: {
        title: "",
        price: "",
        image_url: "",
        event_id: "",
      },
      dialogVisible: false,
      eventList: [],
    };
  },
  components: {
    pagination: () => import("@/components/pagination.vue"),
    upload: () => import("./upLoadWork.vue"),
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.$post("/user/images_list", {
        page: this.page,
        rows: this.rows,
        switch: this.radio,
      }).then((res) => {
        this.list = res.data.data;
        this.total = res.data.total;
      });
    },
    getEveltList() {
      this.$post("/index/events_list", {
        page: 1,
        rows: 999,
      }).then((res) => {
        this.eventList = res.data.data;
      });
    },
    listRefresh() {
      this.page = 1;
      this.radio = "1";
      this.getList();
    },
    handleCurrentChange(e) {
      this.page = e;
      this.getList();
    },
    open() {
      this.$refs.uploadRef.open();
    },
    detail(item) {
      this.getEveltList();
      this.ruleForm.title = item.title_cn;
      this.ruleForm.event_id = item.event_id;
      this.ruleForm.image_url = item.image_url;
      this.ruleForm.price = item.price_cn;
      this.ruleForm.image_sn = item.image_sn;

      this.dialogVisible = true;
    },
    handleShow(item, tips) {
      this.$confirm(tips, "提示信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$post("/user/images_switch", {
            image_sn: item.image_sn,
          }).then((res) => {
            if (res.code == 1) {
              this.$message.success("操作成功");
              this.page = 1;
              this.getList();
            }
          });
        })
        .catch(() => {
          // 用户点击取消按钮或关闭对话框时执行
        });
    },
    imagesUpdate() {
      this.$post("/user/images_update", {
        ...this.ruleForm,
      }).then((res) => {
        if (res.code === 1) {
          this.$message.success("保存成功");
          this.dialogVisible = false;
          this.getList();
        } else {
          this.$message.error("保存失败");
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.tabs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .btn {
    color: #fff;
    background: #917bbd;
    font-size: 14px;
    padding: 6px 20px;
    border-radius: 4px;
  }
}
.title {
  font-size: 24px;
}
.list {
  display: flex;
  flex-wrap: wrap;
  .col {
    width: calc(100% / 3);
    padding: 5px;
    .imgDiv {
      height: 156px;
    }
    .item {
      border-radius: 8px;
      position: relative;
      overflow: hidden;
    }
    .option {
      position: absolute;
      background: rgba($color: #000000, $alpha: 0.8);
      color: #fff;
      display: flex;
      align-items: center;
      width: 100%;
      left: 0;
      bottom: 0;
      height: 40px;
      // &::before {
      //   content: "";
      //   width: 1px;
      //   height: 60%;
      //   background: #fff;
      //   top: 20%;
      //   position: absolute;
      //   left: 50%;
      //   opacity: 0.8;
      // }
      .btn {
        text-align: center;
        width: 100%;
        font-size: 16px;
      }
    }
  }
}
@media screen and (max-width: 768px) {
}
</style>
